<!DOCTYPE html>
<html lang="en">
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap 的 CSS 文件 -->
    <link rel="stylesheet" th:href="@{/css/bootstrap4.6.1.css}">
    <!-- 选项 1：jQuery 和 Bootstrap 集成包（集成了 Popper） -->
    <script th:src="@{/js/jquery3.5.1.js}"></script>
    <script th:src="@{/js/bootstrap4.6.1.js}"></script>
    <script type="text/javascript" th:src="@{/js/jquery-1.11.3.min.js}"></script>
</head>


<style>
    body, html {
        width: 100%;
        height: 100%;
        background: #fff;
    }

    #editor {
        height: 200px;
    }
</style>
<body>

<div id="main-content" class="clearfix" style="margin-left: 0">
    <div id="page-content" class="clearfix">
        <div class="page-header position-relative">
            <h1 style="color: #2679b5;">品牌商
                <small><i class="icon-double-angle-right"></i> 商品信息录入</small>
            </h1>
        </div>

        <form class="form-horizontal">


            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text">商品标题</span>
                </div>
                <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)" id="title">
                <div class="input-group-append">
                    <span class="input-group-text">需包含搜索关键字、品牌名、颜色、大小、型号</span>
                </div>
            </div>
            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text" id="inputGroupFileAddon01">上传图片</span>
                </div>
                <div class="custom-file">
                    <input type="file" class="custom-file-input" id="file" aria-describedby="inputGroupFileAddon01" onchange="ima()" allowexts="gif,jpeg,jpg,png,bmp" accept="image/*">>
                    <label class="custom-file-label" for="file" id="filename">点击上传</label>
                </div>
            </div>

            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text">商品库存</span>
                </div>
                <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)" id="goodsnum">

            </div>

            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text">商品品牌</span>
                </div>
                <div class="controls" >
                    <select id="brandname" class="form-control" style="height: 40px;width: 1350px;">
                    </select>
                </div>
            </div>


            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text">商品类别</span>
                </div>
                <div class="controls">
                    <select id="classification" class="form-control" style="height: 40px;width: 1350px;">
                    </select>
                </div>
            </div>


            <div class="input-group-prepend" style="margin-bottom: 0">
                <span class="input-group-text " style="width: 100%">体积重:指邮购零售包装体积重，以仓库实际测量为准。( 100厘米=39.3700787英寸，1千克=2.2046226磅)</span>
            </div>
            <div class="input-group mb-3" style="float: left;width: 20%;">
                <div class="input-group-prepend">
                    <span class="input-group-text">长 Length(cm)</span>
                </div>
                <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)" id="length" required value="0" >
            </div>

            <div class="input-group mb-3" style="float: left;width: 20%;margin-left: 95px">
                <div class="input-group-prepend">
                    <span class="input-group-text">宽 Width(cm)</span>
                </div>
                <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)"  id="width" required value="0">
            </div>

            <div class="input-group mb-3" style="float: left;width: 20%;margin-left: 95px">
                <div class="input-group-prepend">
                    <span class="input-group-text">高 Height(cm)</span>
                </div>
                <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)"  id="height" required value="0">
            </div>

            <div class="input-group mb-3" style="float: left;width: 20%;margin-left: 95px">
                <div class="input-group-prepend">
                    <span class="input-group-text">重量 Weight(kg)</span>
                </div>
                <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)"  id="weight" required value="0">
            </div>

            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text" style="width: 135px">商品sku编码</span>
                </div>
                <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)" id="sku">
            </div>


            <div class="input-group-prepend" >
                <span class="input-group-text" style="width: 100%;border-bottom: none;background-color: #ffc107;">品牌商可以选择不提供UPC和EAN码
                eBay网店不一定需要UPC码，amazon网店通常自己再另外为借卖商品申请UPC码，
                    这样可以让商品独立销售</span>
                </div>

            <div class="input-group-prepend" >
                <span class="input-group-text" style="width: 100%;border-top: none;background-color: #ffc107;">如果amazon网店不为借卖商品另外申请UPC码，将以跟卖商品销售商品</span>
            </div>

            <div class="input-group-prepend" style="margin-top: 1rem">
                <span class="input-group-text" style="width: 100%;border-bottom: none;">UPC码也是用于商品的条码，主要用于美国和加拿大地区，UPC条码是由美国统一代码委员会制定的一种条码。出口到北美地区为了适应北美地区的需要，也需要申请UPC条码</span>
                </div>

            <div class="input-group-prepend" >
                <span  class="input-group-text" style="width: 100%;border-top: none">UPC条码也有标准版和缩短版两种，标准版由12位数字构成，缩短版由8位数字构成。</span>
            </div>
            <div class="input-group mb-3" style="margin-bottom: 0px">
                <div class="input-group-prepend">
                    <span class="input-group-text" style="width: 135px">upc编码</span>
                </div>
                <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)" id="upc">
            </div>


            <div class="input-group-prepend" >
                <span class="input-group-text" style="width: 100%;border-bottom: none">EAN码(英文全称: European Article
                        Number)由前缀码、厂商识别码、商品项目代码和校验码组成。前缀码是国际EAN组织标识各会员组织的代码，我国为690-695标准版表示13位数字
                </span>
                </div>
            <div class="input-group-prepend">
                <span class="input-group-text" style="width: 100%;border-top: none">又称为EAN13码，缩短码表示8位数字，又称EAN8。两种条码的最后一位为校验码，由前面的12位或7位数字计算得出</span>
            </div>
            <div class="input-group mb-3" style="margin-bottom: 0px">
                <div class="input-group-prepend">
                    <span class="input-group-text" style="width: 135px">ena编码</span>
                </div>
                <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)" id="ena">
            </div>


            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text">商品型号Model</span>
                </div>
                <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)" id="model">
            </div>


            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text" style="width: 135px">借卖价格</span>
                </div>
                <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)" id="price">
                <div class="input-group-prepend">
                    <span class="input-group-text">商品正式上架前，请再审阅借卖价格，并选择借卖价格有效期</span>
                </div>
            </div>
            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text" style="width: 135px">保修期</span>
                </div>
                <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)" id="warrantyTime">
                <div class="input-group-prepend">
                    <span class="input-group-text" style="width: 458px">商品质保承诺将大大提升市场信心（可不提供）</span>
                </div>
            </div>



            <div class="form-group">
                <label for="ebayDescription">eBay description         注意：在eBay网店，商品详情可以利用图片+文字的方式推送</label>
                <textarea class="form-control"  rows="3" id="ebayDescription"></textarea>
            </div>



            <div class="form-group">
                <label for="amazonDescription">Amazon description         注意：在eBay网店，商品详情可以利用图片+文字的方式推送</label>
                <textarea class="form-control"  rows="3" id="amazonDescription"></textarea>
            </div>


        </form>
        <div style="text-align: center">
            <button type="button" class="btn btn-secondary" onclick="javascript:history.back(-1)">取消</button>
            <button type="button" class="btn btn-primary" onclick="addnewpro()">添加</button>
        </div>
    </div>

</div>

<script>
    var mvo_id
    var pic = ''

    //加載新增的商品图片文件名
    function ima() {
        var fileName = $("#file").val();
        var strFileName = fileName.substring(fileName.lastIndexOf("\\") + 1);
        pic = strFileName
        $("#filename").html(pic);
        console.log(pic)
    }

    $(function () {
        var url = location.search;
        var Request = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1)　//去掉?号
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                Request[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }

        mvo_id = Request["uid"];
        console.log(mvo_id);
        //将需要修改信息的商品ID传到该页面
        // $("#index").attr('href','/role?uid='+mvo_id);
    })


    var classlist
    var brandlish

    function addnewpro() {
        var num = $('#goodsnum').val()
        var title = $('#title').val()
        var length = $('#length').val()
        var width = $('#width').val()
        var height = $('#height').val()
        var weight = $('#weight').val()
        var sku = $('#sku').val()
        var upc = $('#upc').val()
        var ena = $('#ena').val()
        var model = $('#model').val()
        var price = $('#price').val()
        var warrantyTime = $('#warrantyTime').val()
        var ebayDescription = $('#ebayDescription').val()
        var amazonDescription = $('#amazonDescription').val()
        var brandname = $('#brandname').val()
        var brandclassificationname = $('#classification').val()

        //不插入图片添加商品信息
        if (pic.length == 0) {
            $.ajax({
                type: "post",
                url: "/goods/addGoods",
                dataType: "text",
                async: false,
                data: {
                    title: title,
                    length: length,
                    width: width,
                    height: height,
                    weight: weight,
                    sku: sku,
                    upc: upc,
                    ena: ena,
                    model: model,
                    price: price,
                    warrantly_time: warrantyTime,
                    ebay_description: ebayDescription,
                    amazon_description: amazonDescription,
                    brandclassificationname: brandclassificationname,
                    brandname: brandname
                },

                success: function (re) {
                    //向Save_goods表增加该商品的库存
                    $(function () {
                        $.ajax({
                            //通过商品标题获取商品id
                            type: 'post',
                            url: '/goods/selectGoodsByTitle',
                            data: {title: title},
                            dataType: 'json',
                            async: false,
                            success: function (good) {
                                // alert(key.goodsNum)
                                console.log(good.title)
                                $.ajax({
                                    type: 'post',
                                    url: '/savegoods/insertSaveGoods',
                                    data: {goodsID: good.id, storehouseID: mvo_id, goodsNum: num},
                                    dataType: 'text',
                                    async: false,
                                    success: function (re) {
                                        console.log(re + ' okshiyoufucker')
                                    },
                                    error: function () {
                                        console.log('nogoodno')
                                    }
                                })
                            },
                            error: function (good) {
                                alert('更新库存出错 ' + good.title + ' ' + title)
                            }
                        })
                    })
                    alert(re)
                    window.location.href = '/mvouploadgoods?uid=' + mvo_id;

                },
                error: function () {
                    alert("出错了")
                }
            })
        } else {//有图片添加
            $.ajax({
                type: "post",
                url: "/goods/addGoodsWithPic",
                dataType: "text",
                async: false,
                data: {
                    title: title,
                    length: length,
                    width: width,
                    height: height,
                    weight: weight,
                    sku: sku,
                    upc: upc,
                    ena: ena,
                    model: model,
                    price: price,
                    warrantly_time: warrantyTime,
                    ebay_description: ebayDescription,
                    amazon_description: amazonDescription,
                    brandclassificationname: brandclassificationname,
                    brandname: brandname,
                    image: pic
                },

                success: function (re) {
                    console.log(pic)
                    //同時向Save_goods表增加該商品的庫存
                    $(function () {
                        $.ajax({
                            //通過商品標題獲取商品的ID
                            type: 'post',
                            url: '/goods/selectGoodsByTitle',
                            data: {title: title},
                            dataType: 'json',
                            async: false,
                            success: function (good) {
                                // alert(key.goodsNum)
                                console.log(good.title)
                                $.ajax({
                                    type: 'post',
                                    url: '/savegoods/insertSaveGoods',
                                    data: {goodsID: good.id, storehouseID: mvo_id, goodsNum: num},
                                    dataType: 'text',
                                    async: false,
                                    success: function (re) {
                                        console.log(re + ' okshiyoufucker')
                                    },
                                    error: function () {
                                        console.log('nogoodno')
                                    }
                                })
                            },
                            error: function (good) {
                                alert('更新库存出错 ' + good.title + ' ' + title)
                            }
                        })
                    })
                    alert(re)
                    window.location.href = '/mvouploadgoods?uid=' + mvo_id;

                },
                error: function () {
                    alert("有问题")
                    // if(length == "" || width == "" || height == "" || weight == ""){alert("体积重不能为空")}
                }
            })
        }


        // window.location.href('/newsType?uid=' + mvo_id)
    }


    $(function () {
        var $selec = $('#brandname');
        console.log(mvo_id + "test");
        $.ajax({
            type: 'post',
            url: '/brand/selectBrandByUserID',
            data: {mvo_id: mvo_id},
            dataType: 'json',
            success: function (brand) {
                brandlish = brand;
                console.log(brand);
                var newHtml = '';
                for (var i = 0; i < brand.length; i++) {
                    newHtml += '<option>' + brand[i].name + '</option>';
                }
                $selec.html(newHtml)
            }
        })
    })

    $(function () {
        var $clazz = $('#classification')
        $.ajax({
            type: 'post',
            url: '/classification/getAllClassification',
            data: {},
            dataType: 'json',
            success: function (list) {
                classlist = list;
                console.log(list);
                var newHtml = '';
                for (var i = 0; i < list.length; i++) {
                    newHtml += '<option>' + list[i].name + '</option>';
                }
                $clazz.html(newHtml)
            }
        })
    })

</script>
</body>
</html>
